package com.crazyauntzhang.easyshopbackend.service;

import com.crazyauntzhang.easyshopbackend.dao.TurnoverFlowDao;
import com.crazyauntzhang.easyshopbackend.domain.TurnoverFlow;
import com.crazyauntzhang.easyshopbackend.result.FindTurnoverResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TurnoverFlowService {

	private final TurnoverFlowDao turnoverFlowDao;

	@Autowired
	public TurnoverFlowService(TurnoverFlowDao turnoverFlowDao) {
		this.turnoverFlowDao = turnoverFlowDao;
	}

	/**
	 * 添加流水数据
	 *
	 * @param sequenceNo       流水号
	 * @param sequenceDateTime 流水时间
	 * @param operation        操作
	 * @param userID           收银员ID
	 * @param itemBarcode      商品条码
	 * @param price            商品单价
	 * @param quantity         购买数量
	 * @param profit           单品收益
	 * @param handlerID        经手人
	 * @param purchaseNo       退款来源
	 */
	public void addFlow(
			String sequenceNo,
			LocalDateTime sequenceDateTime,
			String userID,
			String itemBarcode,
			Integer operation,
			Double price,
			Double quantity,
			Double profit,
			String handlerID,
			String purchaseNo) {
		turnoverFlowDao.insertFlow(
				sequenceNo,
				sequenceDateTime,
				userID,
				itemBarcode,
				operation,
				price,
				quantity,
				profit,
				handlerID,
				purchaseNo);
	}

	public List<TurnoverFlow> findTurnoverFlow(String sequenceNo) {
		return turnoverFlowDao.selectFlow(sequenceNo);
	}

	public TurnoverFlow findFlowWithItemBarcode(String sequenceNo, String itemBarcode) {
		return turnoverFlowDao.selectFlowWithItemBarcode(sequenceNo, itemBarcode);
	}

	public Double findSalesIn30Days(String itemBarcode) {
		return turnoverFlowDao.selectSalesIn30Days(itemBarcode);
	}

	public List<String> findTop20SalesIn30DaysWithBarcode() {
		return turnoverFlowDao.selectTop20SalesIn30DaysWithBarcode();
	}

	public List<FindTurnoverResponse> findTodayTurnover() {
		return turnoverFlowDao.selectTodayTurnover();
	}

	public List<FindTurnoverResponse> findPeriodTurnover(LocalDate beginDate, LocalDate endDate) {
		return turnoverFlowDao.selectPeriodTurnover(beginDate, endDate);
	}

	public Double findRestQuantity(String sequenceNo, String itemBarcode) {
		return turnoverFlowDao.selectRestQuantity(sequenceNo, itemBarcode);
	}

}
